﻿using DevExpress.Data.Filtering;
using DevExpress.Mvvm;
using HandyControl.Controls;
using HandyControl.Data;
using log4net;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.Services.Instances;
using PlaceOrder_Core.Services.Interfaces;
using PlaceOrder_Server.Events;
using PlaceOrder_Server.Extensions;
using PlaceOrder_Shared.Models.Common;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using BindableBase = Prism.Mvvm.BindableBase;
using MessageBox = HandyControl.Controls.MessageBox;

namespace PlaceOrder_Server.ViewModels
{
    internal class MainWindowViewModel : BindableBase
    {
        private readonly ILog _logger;
        private readonly IRegionManager _regionManager;
        private readonly IUserHelper _userHelper;

        public string Title { get; set; } = "全益订单管理服务端";

        public DelegateCommand TestCommand { get; set; }
        public MainWindowViewModel(ILog logger, IEventAggregator aggregator, IRegionManager regionManager,UserHelper userHelper)
        {
            //注册提示消息
            _userHelper = userHelper;
            aggregator.GetEvent<MessageEvent>().Subscribe(arg =>
            {

                //Growl.SuccessGlobal("11111",);

                Growl.Info(arg.Message, "SuccessMsg");


            });
            _logger = logger;
            _logger.Info("MainWindowViewModel构造函数");

            this._regionManager = regionManager;
            //Configure();
            TestCommand = new DelegateCommand(() => Configure());
            //_regionManager.Regions[PrismRegionManager.MainViewRegionName].RequestNavigate("IndexServerView");
            Initialize();
        }
        private async void Initialize()
        {
            // 等待5秒后执行 Configure 方法

            await Login();
            await Task.Delay(1000);
            Configure();

            // 初始化命令
            //TestCommand = new DelegateCommand(() => Configure());
        }
        async Task Login()
        {

            AppLog.WriteInfo("登录成功哦");


            UserInfo userInfo = new UserInfo();
            userInfo.userName = "admin";
            userInfo.password = "admin123";

            var loginResult = await _userHelper.Login(userInfo, 0);
          

            if (loginResult.Item2)
            {
                if (UserHelper.UserName != "admin")
                {
                    Growl.Info("请使用admin登录账户", "LoginMsg");
                    return;
                }
            }
            else
            {
                Growl.Error("登录异常,请检查用户名及密码", "LoginMsg");
            }

            if (loginResult.Item1 != null && loginResult.Item2 == true)
            {
                string msg = "登录成功。" + "账户为：";
               
                AppLog.WriteInfo(msg);
            }
            else
            {
               
                //AppLog.WriteError(msg);
      
            }
        }
        public void Configure()
        {
            //默认导航到IndexView首页

            Thread.Sleep(1000);
            _regionManager.RequestNavigate("MainViewRegion", "IndexServerView", result =>
            {
                if ((bool)!result.Result)
                {
                    // 处理导航失败的情况
                    MessageBox.Show("Navigation failed: " + result.Error.Message);
                }
            });
        }
    }
}
